package com.jzo2o.orders.manager.Handler.OrdersHandler;

import com.jzo2o.orders.base.model.domain.Orders;
import com.jzo2o.orders.manager.model.dto.request.PlaceOrderReqDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 订单处理完成后续操作Handler
 */
@Slf4j
@Component
@Order(50)  // 最后一个执行
public class OrderAfterHandlerNew extends OrdersHandlerNew {

    @Resource
    private RabbitTemplate rabbitTemplate;

    @Override
    public void process(Orders order, PlaceOrderReqDTO reqDTO) {
        log.info("订单处理完成后续操作Handler");
        // 这里可以添加后续操作，如发送消息、记录日志等

        //发送mq消息,处理超时未支付问题
        log.info("发送mq消息,处理超时未支付问题");
        String orderId = order.getId() + "";
        Message message = MessageBuilder
                .withBody(orderId.getBytes())
                //延迟15分钟
                .setHeader("x-delay", 15*60*1000)
                .build();
        rabbitTemplate.convertAndSend("order-exchange", "order-timeout-queue", message);
    }

    @Override
    public int getOrder() {
        return 50;
    }
}